//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using FlickrNet;
using Mindscape.BackgroundMotion.Core.Providers;
using Mindscape.BackgroundMotion.Website.Infrastructure;

namespace Mindscape.BackgroundMotion.Website.Resources
{
  /// <summary>
  /// An implementation of a resource provider to allow us to connect to Flickr
  /// </summary>
  public class Flickr : IResourceProvider
  {
    /// <summary>
    /// Returns the details associated with a resource returned by a Flickr search
    /// </summary>
    /// <param name="identifier"></param>
    /// <returns></returns>
    public ResourceDetails GetDetails(string identifier)
    {
      FlickrNet.Flickr flickr = new FlickrNet.Flickr();
      ResourceDetails details = new ResourceDetails();

      details.ContentType = Constants.ContentTypeIds.Image;
      PhotoInfo photoInfo = flickr.PhotosGetInfo(identifier);
      details.Title = photoInfo.Title;
      details.Description = photoInfo.Description;
      details.Url = photoInfo.WebUrl;
      details.DownloadUrl = photoInfo.LargeUrl;
      details.ThumbnailUrl = photoInfo.SmallUrl;
      
      if (photoInfo.Location != null)
      {
        details.GeoX = photoInfo.Location.Latitude;
        details.GeoY = photoInfo.Location.Longitude;
      }

      if (photoInfo.Tags != null && photoInfo.Tags.TagCollection != null)
      {
        details.Tags = new string[photoInfo.Tags.TagCollection.Length];

        for (int i = 0; i < photoInfo.Tags.TagCollection.Length; i++ )
        {
          details.Tags[i] = photoInfo.Tags.TagCollection[i].TagText;
        }        
      }
      
      return details;
    }
  }
}
